package com.cskaoyan.service.wx;

import com.cskaoyan.bean.MarketAddress;
import com.cskaoyan.bean.MarketAddressExample;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.model.vx.vo.WxAddressDetailRespVO;
import com.cskaoyan.model.vx.vo.WxAddressListRespVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName: WxAddressServiceImpl
 * @Author: WangEnzhao
 * @Description: 微信地址管理
 * @CreateTime: 2021/12/11:24:00
 */
@Service
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    MarketAddressMapper marketAddressMapper;


    /***
    * @Author: WangEnzhao
    * @Description: 查询微信地址
    * @CreateTime: 11:43 12/2/2021
    * @Param: [id]
    * @return: com.cskaoyan.model.vx.vo.WxAddressListRespVO
    */
    @Override
    public WxAddressListRespVO addressList(Integer userId) {
        //返回的数据
        WxAddressListRespVO wxAddressListRespVO = new WxAddressListRespVO();

        //查询
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        marketAddressExample.createCriteria().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);

        //赋值
        wxAddressListRespVO.setLimit(1);
        wxAddressListRespVO.setPage(1);
        wxAddressListRespVO.setPages(1);
        wxAddressListRespVO.setTotal(marketAddresses.size());
        wxAddressListRespVO.setList(marketAddresses);

        return wxAddressListRespVO;
    }


    /***
    * @Author: WangEnzhao
    * @Description: 新建地址
    * @CreateTime: 14:51 12/2/2021
    * @Param: [marketAddress]
    * @return: java.lang.Integer
    */
    @Transactional
    @Override
    public Integer addressSave(MarketAddress marketAddress) {
        if(marketAddress.getIsDefault() == true){
            //如果是默认收货地址，需要将之前默认收货地址改为false
            setDefault2False(marketAddress.getUserId());
        }
        int affectedRows = marketAddressMapper.insertSelective(marketAddress);
        return marketAddress.getId();
    }


    /***
    * @Author: WangEnzhao
    * @Description: 将表中的default为true的设置为false
    * @CreateTime: 16:59 12/2/2021
    * @Param: []
    * @return: void
    */
    private void setDefault2False(Integer userId) {
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        marketAddressExample.createCriteria().andIsDefaultEqualTo(true).andUserIdEqualTo(userId);
        MarketAddress record = new MarketAddress();
        record.setIsDefault(false);
        marketAddressMapper.updateByExampleSelective(record,marketAddressExample);
    }


    /***
    * @Author: WangEnzhao
    * @Description: 删除地址
    * @CreateTime: 14:52 12/2/2021
    * @Param: [id, userId]
    * @return: java.lang.Integer
    */
    @Transactional
    @Override
    public Integer addressDelete(Integer id, Integer userId) {
        if(userId == null || id == null){
            return 0;
        }

        //创建record
        MarketAddress record = new MarketAddress();
        record.setDeleted(true);
        //创建删除条件
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        marketAddressExample.createCriteria().andUserIdEqualTo(userId).andIdEqualTo(id);
        int affectedRows = marketAddressMapper.updateByExampleSelective(record,marketAddressExample);
        return affectedRows;
    }

    @Override
    public MarketAddress addressDetail(Integer id, Integer userId) {
        MarketAddress marketAddress = new MarketAddress();
        //判断id和userId是否合法
        if(id == null || userId == null){
            return marketAddress;
        }
        //搜索信息
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        marketAddressExample.createCriteria().andUserIdEqualTo(userId).andIdEqualTo(id);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);
        if(marketAddresses.size() == 1){
            marketAddress = marketAddresses.get(0);
        }
        return marketAddress;
    }

    @Transactional
    @Override
    public Integer addressUpdate(MarketAddress marketAddress) {
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        marketAddressExample.createCriteria().andIdEqualTo(marketAddress.getId()).
                andUserIdEqualTo(marketAddress.getUserId());

        if(marketAddress.getIsDefault() == true){
            //如果是默认收货地址，需要将之前默认收货地址改为false
            setDefault2False(marketAddress.getUserId());
        }
        marketAddressMapper.updateByExampleSelective(marketAddress,marketAddressExample);

        return marketAddress.getId();
    }
}
